/*
 * @Description: 文章发布
 * @Author: charles
 * @Date: 2021-11-16 11:15:27
 * @LastEditors: charles
 * @LastEditTime: 2021-11-16 12:03:26
 */
import qs from 'qs'
import {Button,Form,Input,message} from 'antd'
import {token} from '../utils/config'
import axios from 'axios';
import _ from 'lodash'
function ArticlePublish(props){
  
  let article = {};
  if(props.location.state){
    article = _.cloneDeep(props.location.state);
  }
  console.log('原值',article);
  
  const backHandler = ()=>{
    props.history.go(-1);
  }
  const onFinish =(value)=>{
    let url = "http://121.199.29.84:8002/article/saveOrUpdate"
    value = Object.assign({},article,value)
    console.log("修改后的值",value);
    axios.post(url,qs.stringify(value),{
      headers:{
        Authorization:token,
        "Content-Type":"application/x-www-form-urlencoded"
      }
    }).then(resp => {
      //{ data:{message,status,data},headers:{},config:{}}
      message.success(resp.data.message)
      backHandler();
    })
  }

  return(
    <div>
      <h2>
        { article?"资讯修改":"资讯发布" }
      </h2>
      <div style={{marginBottom:"1em"}}>
        <Button onClick={backHandler}>返回</Button>
      </div>
      <Form
        name="basic"
        initialValues={article }
        onFinish={onFinish}
      >
        <Form.Item
          label="标题"
          name="title"
          rules={[ { required: true, message: '请输入标题!', }, ]}
        >
          <Input />
        </Form.Item>
        <Form.Item
          label="内容"
          name="content"
          rules={[ { required: true, message: '请输入内容!', }, ]}
        >
          <Input.TextArea rows={5} />
        </Form.Item>

        <Form.Item >
          <Button type="primary" htmlType="submit">
            Submit
          </Button>
        </Form.Item>
      </Form>
    </div>
  )
}
export default ArticlePublish;
